package com.uzai.console.web.cms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.cms.activity.CmsActivityDetailInfoDto;
import com.uzai.console.dto.cms.activity.CmsActivityDetailQuery;
import com.uzai.console.dto.cms.homepage.CmsHomePageConfigDto;
import com.uzai.console.dto.cms.homepage.CmsHomePageGetConfigDto;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.service.cms.CmsActivityService;
import com.uzai.console.service.cms.CmsHomePageService;
import com.uzai.console.service.sysmaterial.SysMaterialService;
import com.uzai.console.vo.cms.activity.CmsActivityDetailInfoVo;
import com.uzai.console.vo.cms.homepage.CmsHomePageConfigVo;
import com.uzai.console.vo.feign.uzaicms.CMSBanner;
import com.uzai.console.vo.feign.uzaicms.HomeActNavEntity;
import com.uzai.console.vo.sysmaterial.UploadFileVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;

/**
 * CMS小程序|首页设置
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "首页设置",tags = "CMS小程序|首页设置")
@ApiSort(7)
@RestController
public class CmsHomePageController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(CmsHomePageController.class);

    @Autowired
    private CmsHomePageService cmsHomePageService;
    @Autowired
    private CmsActivityService cmsActivityService;
    @Autowired
    private SysMaterialService sysMaterialService;
    @Autowired
    private UzaiConsoleSysConfig sysConfig;


    @SystemLogType(model = SystemModelEnum.CmsHomePage, type = OperationTypeEnum.UPLOAD)
    @ApiOperation(value = "上传二维码图片", notes = "上传二维码图片", response = UploadFileVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/cmsBase/uploadQrcode")
    public Object uploadQrcode(MultipartFile file) {
        Long merId = getLoginInfo().getMerId();
        return sysMaterialService.uploadFile(file, merId);
    }

    @ApiOperation(value = "分页查询活动展示位置信息列表", notes = "分页查询活动展示位置信息列表", response = CmsActivityDetailInfoVo.class)
    @ApiOperationSupport(order = 2)
    @PostMapping("/cmsBase/findCmsActivityDetailList")
    public Object findCmsActivityDetailList(@RequestBody CmsActivityDetailQuery cmsActivityDetailQuery){
        return cmsActivityService.findCmsActivityDetailList(cmsActivityDetailQuery);
    }

    @SystemLogType(model = SystemModelEnum.CmsHomePage, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "编辑CMS活动展示位置配置详情", notes = "编辑CMS活动展示位置配置详情")
    @ApiOperationSupport(order = 3)
    @PostMapping("/cmsBase/updateCmsActivityDetail")
    public Object updateCmsActivityDetail(@RequestBody CmsActivityDetailInfoDto cmsActivityDetailInfoDto){
        cmsActivityService.updateCmsActivityDetail(cmsActivityDetailInfoDto);
        return true;
    }

    @ApiOperation(value = "查询CMS首页配置信息", notes = "查询CMS首页配置信息", response = CmsHomePageConfigVo.class)
    @ApiOperationSupport(order = 4)
    @PostMapping("/cmsBase/findCmsHomePageInfo")
    public Object findCmsHomePageInfo(){
        return cmsHomePageService.findCmsHomePageInfo(getLoginInfo().getMerId());
    }

    @SystemLogType(model = SystemModelEnum.CmsHomePage, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "保存CMS首页设置信息", notes = "保存CMS首页设置信息")
    @ApiOperationSupport(order = 5)
    @PostMapping("/cmsBase/updateCmsHomePage")
    public Object updateCmsHomePage(@RequestBody CmsHomePageConfigDto cmsHomePageConfigDto){
        cmsHomePageService.updateCmsHomePage(cmsHomePageConfigDto);
        return true;
    }

    @ApiOperation(value = "查询首页轮播图片列表", notes = "查询首页轮播图片列表", response = CMSBanner.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/cmsBase/findHomeActCarouseList")
    public Object findHomeActCarouseList(){
        return cmsHomePageService.findHomeActCarouseList();
    }

    @ApiOperation(value = "查询首页优惠头条列表", notes = "查询首页优惠头条列表", response = CMSBanner.class)
    @ApiOperationSupport(order = 7)
    @PostMapping("/cmsBase/findHomeActDiscountHeadlineList")
    public Object findHomeActDiscountHeadlineList() {
        return cmsHomePageService.findHomeActDiscountHeadlineList();
    }

    @ApiOperation(value = "查询首页活动配置", notes = "查询首页活动配置", response = HomeActNavEntity.class)
    @ApiOperationSupport(order = 8)
    @PostMapping("/cmsBase/findHomeActNavList")
    public Object findHomeActNavList() {
        return cmsHomePageService.findHomeActNavList();
    }

    @ApiOperation(value = "获取官方活动配置-根据类型", notes = "获取官方活动配置-根据类型", response = JSONObject.class)
    @ApiOperationSupport(order = 8)
    @PostMapping("/cmsBase/findOfficial")
    public Object findOfficial(@RequestBody CmsHomePageGetConfigDto getConfigDto) {
        if (Objects.isNull(getConfigDto) || Objects.isNull(getConfigDto.getType())) {
            throw new BusinessException("参数错误");
        }
        Integer type = getConfigDto.getType();
        Long defaultMerId = sysConfig.getDefaultMerId();
        CmsHomePageConfigVo cmsHomePageInfo = cmsHomePageService.findCmsHomePageInfo(defaultMerId);
        JSONObject config = JSON.parseObject(cmsHomePageInfo.getCmsHomePageConfig());
        JSONArray components = config.getJSONArray("components");
        for (int i = 0; i < components.size(); i++) {
            JSONObject jsonObject = components.getJSONObject(i);
            if (Objects.equals(type, 1)) {
                if (!"PicWord".equals(jsonObject.getString("name"))) {
                    continue;
                }
                return jsonObject.getJSONObject("config");
            } else if (Objects.equals(type, 2)) {
                if (!"MarketNavigate".equals(jsonObject.getString("name"))) {
                    continue;
                }
                return jsonObject.getJSONObject("config");
            } else if (Objects.equals(type, 3)) {
                if (!"MostOne".equals(jsonObject.getString("name"))) {
                    continue;
                }
                return jsonObject.getJSONObject("config");
            }
        }
        return new JSONObject();
    }
}
